package com.wngbms.netty.seatdemo.service;

import com.wngbms.netty.seatdemo.comm.*;
import com.wngbms.netty.seatdemo.message.BaseHeader;
import com.wngbms.netty.seatdemo.message.SeatInMessage;
import com.wngbms.netty.seatdemo.message.SeatOutMessage;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

@Slf4j
public class LargeBinaryStreamDownloadTestHandler implements SeatTypeHandler {
    public static final CmdType CMD_TYPE = CmdType.BINARY_TEST_DOWNLOAD;

    @Override
    public void handleInbound(SeatInMessage seatInMessage) {
        log.info("客户端文件下载测试");

//        String filePath = "J:\\mini.txt";
//        String filePath = "J:\\readme.txt";
//        String filePath = "J:\\img0.jpg";
//        String filePath = "J:\\node-v14.16.1-x64.msi";
//        String filePath = "J:\\aDrive.exe";
        String filePath = "J:\\mysql-5.7.17.msi";
        File file = new File(filePath);
        if (!file.exists()) {
            log.info("{}文件不存在", filePath);
            return;
        }

        int length = (int) file.length();
        BaseHeader baseHeader = BaseHeader.build(MagicVersion.V1, CMD_TYPE, BodyType.BINARY,
                EncryptionType.NON, ReqOrResp.RESP, length);

        SeatOutMessage seatOutMessage = new SeatOutMessage();
        seatOutMessage.setBaseHeader(baseHeader, file);

        Channel channel = seatInMessage.getChannelHandlerContext().channel();
        channel.writeAndFlush(seatOutMessage);
    }

}
